package com.yc.txx.servlet;

import com.google.gson.Gson;
import com.yc.txx.biz.BizExcpetion;
import com.yc.txx.biz.LeaseBiz;
import com.yc.txx.biz.UserBiz;
import com.yc.txx.entity.Lease;
import com.yc.txx.entity.User;
import com.yc.txx.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;



@WebServlet("/LoginServlet.s")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        UserBiz biz = new UserBiz();
        Result result;

        try {
            User user = biz.login(name, pwd);
           request.getSession().setAttribute("myinfo", user);
            result = Result.success("登陆成功");
        } catch (BizExcpetion e) {
            e.printStackTrace();
            result = Result.failure(e.getMessage());
        }

        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }
}